package pw.hais.etgsh.dao;

import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Response;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import pw.hais.app.UtilConfig;
import pw.hais.etgsh.model.DianTaiMp3Model;
import pw.hais.etgsh.model.DianTaiTypeModel;
import pw.hais.http.Http;
import pw.hais.http.base.OnHttpListener;
import pw.hais.utils.EmptyUtil;

/**
 * 电台
 * Created by hais1992 on 2016/1/23/023.
 */
public class DianTaiDao {
    public static final String SERVER_URL = "http://mp3.61ertong.com/default.php";


    public static void getAllType(final OnHttpListener<List<DianTaiTypeModel>> listener) {
        final List<DianTaiTypeModel> list = new ArrayList<>();
        Http.get(SERVER_URL, null, new OnHttpListener<String>() {
            @Override
            public void onSuccess(Response response, String html) {
                Document document = Jsoup.parse(html);
                //分类1
                Elements elements1 = document.select("#wrapper1").select("div");
                for (int i = 1; i < elements1.size(); i++) {
                    String title = "[儿歌]" + elements1.get(i).select("a").text();
                    String url = elements1.get(i).select("a").attr("href");
                    String content = elements1.get(i).select("p").text();
                    if (!EmptyUtil.emptyOfString(url))
                        list.add(new DianTaiTypeModel(url, title, content));
                }
                //分类2
                Elements elements2 = document.select("#wrapper2").select("div");
                for (int i = 1; i < elements2.size(); i++) {
                    String title = "[故事]" + elements2.get(i).select("a").text();
                    String url = elements2.get(i).select("a").attr("href");
                    String content = elements2.get(i).select("p").text();
                    if (!EmptyUtil.emptyOfString(url))
                        list.add(new DianTaiTypeModel(url, title, content));
                }

                if (listener != null) listener.onSuccess(null, list);
            }
        });
    }


    /**
     * 获取内容详情
     */
    public static void getModelById(final String id, final OnHttpListener<List<DianTaiMp3Model>> listener, final OnHttpListener<String> urlListener) {
        Http.get(id, null, new OnHttpListener<String>() {
            @Override
            public void onSuccess(Response response, String html) {
                doArticleHtmlToItemModel(id, html, listener);
                if(urlListener!=null){
                   String url = Jsoup.parse(html).select("#_ajax_search_thumb").attr("src");
                    urlListener.onSuccess(null,url);
                }
            }
        });
    }


    public static void doArticleHtmlToItemModel(String id, String html, OnHttpListener<List<DianTaiMp3Model>> listener) {
        List<DianTaiMp3Model> list = null;

        Pattern p = Pattern.compile("\\[\\{title(.*)");
        Matcher m = p.matcher(html);
        for (int i = 0; i < m.groupCount(); i++) {
            if (m.find()) {
                try {
                    String message = m.group(i).replace("title", "\"title\"").replace("mp3:", "\"mp3\":").replace("id", "\"id\"").replace("'", "\"").replace("name", "\"name\"").replace("singer", "\"singer\"").replace(";", "");

                    if (!EmptyUtil.emptyOfString(message)) {
                        list = UtilConfig.GSON.fromJson(message, new TypeToken<ArrayList<DianTaiMp3Model>>() {
                        }.getType());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        listener.onSuccess(null, list);

//        Document document = Jsoup.parse(html);
//
//        Elements elements = document.select("script");
//        for (int i = 0; i < elements.size() - 1; i++) {
//            if (elements.get(i).toString().indexOf("mlist") != -1) {
//
//            }
//            L.i("-----" + elements.get(i).toString());
//        }

//        listener.onSuccess(null, article);
    }
}
